package ru.spb.xmlreshelper.screen.cdt;

import com.google.common.eventbus.Subscribe;
import ru.spb.xmlreshelper.XmlResHelper;
import ru.spb.xmlreshelper.domain.CDTRecord;
import ru.spb.xmlreshelper.domain.ICdtService;
import ru.spb.xmlreshelper.gui.AbstractAppPanel;
import ru.spb.xmlreshelper.gui.PanelEvent;
import ru.spb.xmlreshelper.util.GuiUtils;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

/**
 * @author Nikita.Brazhnikov
 */
public class CDTViewPanel extends AbstractAppPanel {


    private JTable cdtTable;
    private CDTTableModel tableModel;

    private static final String[] COLUMNS = {"ID", "NAME", "NAMESPACE", "TYPE"};

    public CDTViewPanel() {
        build();
        GuiUtils.getEventBus().register(this);
    }

    private void build() {
        setLayout(new BorderLayout());

        tableModel = new CDTTableModel();
        cdtTable = new JTable(tableModel);
        cdtTable.setRowSelectionAllowed(true);
        cdtTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cdtTable.setAutoCreateRowSorter(true);
        cdtTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        cdtTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    CDTRecord selectedRecord = tableModel.getRecords().get(cdtTable.convertRowIndexToModel(e.getFirstIndex()));
                    GuiUtils.getEventBus().post(new CDTSelectionEvent("CDT_SELECTION", selectedRecord));
                }
            }
        });
        cdtTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    CDTRecord selectedRecord = tableModel.getRecords().get(cdtTable.convertRowIndexToModel(cdtTable.getSelectedRow()));
                    GuiUtils.getEventBus().post(new CDTSelectionEvent("CDT_SELECTION", selectedRecord).dbClick());
                }
                super.mouseClicked(e);
            }
        });
        final Enumeration<TableColumn> columns = cdtTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn tableColumn = columns.nextElement();
            tableColumn.setPreferredWidth(tableModel.getColumnWidth(tableColumn.getModelIndex()));
        }
        JScrollPane scroll = new JScrollPane(cdtTable);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scroll, BorderLayout.CENTER);
    }

    @Subscribe
    public void processModelUpdate(PanelEvent panelEvent) {
        String eventName = panelEvent.getEventName();
        if (eventName.equals(ICdtService.CDT_LIST_UPDATED)) {
            SwingWorker worker = new SwingWorker() {

                java.util.List<CDTRecord> records;

                @Override
                protected Object doInBackground() throws Exception {
                    ICdtService cdtService = XmlResHelper.getService(ICdtService.class);
                    records = cdtService.getCdts();
                    return null;
                }

                @Override
                protected void done() {
                    tableModel.setModelList(records);
                }
            };
            worker.execute();
        }
    }

}
